package com.teamcalendar.client.controller;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ListView;

import com.teamcalendar.api.CreateJson;
import com.teamcalendar.api.ParseJson;
import com.teamcalendar.client.DBAdapter;

public class MessageActivity extends ListActivity {

	String memberId;
	DBAdapter mDb = new DBAdapter(this);
	Cursor c;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.message);
		Bundle bundle = getIntent().getExtras();
		memberId = bundle.getString("memberId");
		mDb.open();

		listAllMessages();
		registerForContextMenu(getListView());
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		mDb.close();
		c.deactivate();
	}	

	public void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);		
		c = mDb.getMessage(id);
		int read = c.getInt(c.getColumnIndex(DBAdapter.KEY_READ));
		if (read == 0) {
			mDb.updateMessage(id, 
					c.getString(c.getColumnIndex(DBAdapter.KEY_SENDERID)), 
					c.getString(c.getColumnIndex(DBAdapter.KEY_MESSAGE)), 
					c.getString(c.getColumnIndex(DBAdapter.KEY_MESSAGE_ID)), 
					1);
			CreateJson json = new CreateJson();
			json.setJsonObjectType("messageread");
			json.setMessageId(c.getString(c.getColumnIndex(DBAdapter.KEY_MESSAGE_ID)));
			json.createJsonObject();
		}
		c.deactivate();
		Intent	readMessage = new Intent(MessageActivity.this, ReadMessageActivity.class);
		readMessage.putExtra("rowid", id);
		readMessage.putExtra("memberId", memberId);
		startActivity(readMessage);
		listAllMessages();
	}

	public void answer(long rowId) {
		Cursor messageCursor = mDb.getMessage(rowId);
		String message = messageCursor.getString(messageCursor.getColumnIndex(DBAdapter.KEY_MESSAGE));
		String senderId = messageCursor.getString(messageCursor.getColumnIndex(DBAdapter.KEY_SENDERID));
		Intent writeMessage = new Intent(MessageActivity.this, WriteMessageActivity.class);
		writeMessage.putExtra("message", message);
		writeMessage.putExtra("memberId", memberId);
		writeMessage.putExtra("senderid", senderId);
		startActivity(writeMessage);
	}

	//When the WriteNewMessage-button is clicked
	private void writeNewMessage() {
		Intent writeNewMessage = new Intent(MessageActivity.this, WriteMessageActivity.class);
		writeNewMessage.putExtra("memberId", memberId);
		writeNewMessage.putExtra("senderId", "");
		writeNewMessage.putExtra("message", "");
		startActivity(writeNewMessage);
	}
	
	private void update() {
		CreateJson json = new CreateJson();
		json.setJsonObjectType("getmessages");
		json.setMemberId(memberId);
		json.createJsonObject();
		ParseJson pjson = new ParseJson(mDb);
		pjson.fromJSONMessage(json.getResponse());
		listAllMessages();
	}

	/**
	 * Lists all messages to this person
	 */
	private void listAllMessages() {

		ListView lv = (ListView) findViewById(android.R.id.list);
		//
		c = mDb.getAllMessages();

		String[] from = new String[]{DBAdapter.KEY_MESSAGE };
		int[] to = new int[] { R.id.message_text};
		
		MessageAdapter adapter = new MessageAdapter(this, R.layout.message_row, c, from, to);
//		SimpleCursorAdapter messages = new SimpleCursorAdapter(this, R.layout.message_row, c, from, to);
		lv.setAdapter(adapter);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.message_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_message_writemessage:
			writeNewMessage();
			return true;
		case R.id.menu_message_update:
			update();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		menu.clear();

		menu.add(0, R.id.menu_message_writemessage, 0, "Skriv meddelande");
		menu.add(0, R.id.menu_message_update, 0, "Uppdatera listan");
		return super.onPrepareOptionsMenu(menu);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
		switch (item.getItemId()) {
		case R.id.context_menu_answer:
			answer(info.id);
			return true;
		case R.id.context_menu_delete:
			mDb.deleteMessage(info.id);
			listAllMessages();
			return true;
		case R.id.menu_message_writemessage:
			writeNewMessage();
			return true;
		case R.id.menu_message_update:
			update();
			return true;
		default:
			return super.onContextItemSelected(item);
		}
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.clear();
		
		menu.add(0, R.id.context_menu_answer, 0, "Svara");
		menu.add(0, R.id.context_menu_delete, 0, "Ta bort");
		menu.add(0, R.id.menu_message_writemessage, 0, "Skriv meddelande");
		menu.add(0, R.id.menu_message_update, 0, "Uppdatera listan");
	}
}
